﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using JsonTest1.Net;
using JsonTest1.Configure;
using System.IO;
using JsonTest1.Data;
using JsonTest1.Xml;
using JsonTest1.Json;

namespace JsonTest1.NetEngine
{
 
    public class SearchForAlbums : HttpMessage
    {

        public SearchForAlbums()
        {
            base.init();
        }

         public void Request(Constants.Album_Type type,string albumParams)
        {
            //this.OnDataStreamGenerated += new Net.HttpDataStreamCalback(HttpDataStreamCalback);
            String url = "";
            switch (type)
           {
               case Constants.Album_Type.Album_Type_Artist:
                    {
                        url =Constants.GET_API +"id+name+url+image+rating+artist_name/album/json/?order=ratingweek_desc&n=50&searchquery=" + albumParams;
                        break;
                    }
               case Constants.Album_Type.Album_Type_Tag:
                    {
                        url = Constants.GET_API + "id+name+url+image+rating+artist_name/album/json/?order=ratingweek_desc&tag_idstr=" + albumParams + "&n=50";
                        break;
                    }
               case Constants.Album_Type.Album_Type_ArtistName:
                    {
                        url = Constants.GET_API + "id+name+url+image+rating+artist_name/album/json/?order=ratingweek_desc&n=50&artist_name=" + albumParams;
                        break;
                    }
           }
             
            BeginRequest(url);
        }



        public override  bool HttpGeneratorDataCallback(Stream stream){

            try
            {
                //解析xml,过滤错误
                string jsonStr = new StreamReader(stream).ReadToEnd();
                if (jsonStr == null)
                {
                    //AlbumFunctions.getAlbums();
                    HttpFailCalback(1, 1, new ResultData(1, 1, ""));
                }
                else
                {
                    if (httpStateListener != null)
                        httpStateListener.HttpDataStreamCalback(stream, new ResultData(0, 0, AlbumFunctions.getAlbums(jsonStr)));
                }
            }
          /*  catch (JsonException e)
            {
                HttpFailCalback(1, 1, new ResultData(1, 1, ""));
            }*/
            catch (Exception e)
            {

            }

            return true;
        }
 
    }
    
}
